import xlrd
import math
import matplotlib.pyplot as plt
from core.DataStruct import Sample, ColorantGroup, Formula
from core.DataHandler import DataHandler
from core.ColorCombine import ColorCombine

if __name__ == "__main__":
    plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
    # 色料组；
    group = ColorantGroup("油漆")                                         # 染料组；
    # 获取所有的数据；
    hander = DataHandler(group)
    hander.getDataFromExcel("test2.xlsx")                                # 获取数据并提取特征K/Sw, S/Sw, SD;

    # 标样；
    # data = xlrd.open_workbook("PANTON 14-0116standard.xlsx")
    # data = xlrd.open_workbook("16-1120recipe.xlsx")
    data = xlrd.open_workbook("CDC 1062.xlsx")
    sheet = data.sheet_by_index(0)
    for i in range(1, sheet.nrows):            # 每一行是一个标样；
        stdSmp = Sample()
        for j in range(31):
            stdSmp.ref[j] = sheet.cell_value(i, j + 1) * 0.01
        stdSmp.ref[31] = 2 * stdSmp.ref[30] - stdSmp.ref[29]
        stdSmp.calcActualRef(group.K1)
        group.stdSmp = stdSmp
        colorants = group.colorants[1:4]                    # 红，黄， 绿， 蓝， 黑；

        formula = Formula()                                                 # 配方；
        formula.nComp = 4
        formula.colorantsComp[0].colorants = group.pureWhite                # 选择四种色料；
        formula.colorantsComp[1].colorants = group.colorants[0]             # 红
        formula.colorantsComp[2].colorants = group.colorants[1]             # 黄
        # formula.colorantsComp[2].colorants = group.colorants[2]             # 绿
        formula.colorantsComp[3].colorants = group.colorants[3]             # 蓝

        # 配色；
        colorCombine = ColorCombine(group, formula)
        colorCombine.setStandardSmp(stdSmp)

        tmpName = sheet.cell_value(i, 0)
        base = group.baseWhite

        # testConc = colorCombine.calcConcBySpider(base)                                 # 配色；
        testConc = [17.1530, 1.0638, 1.3175, 0.4657]
        recipe = Sample()
        recipe.loadSmp("CDC1062recipe2.xlsx")
        # 调节因子；
        factor = colorCombine.calcRefByConc(formula, testConc, group.K1, group.pureWhite, base, recipe)

        recipe.calcDeltaE(stdSmp)
        deltaXYZ = [0] * 3
        for k in range(3):
            deltaXYZ[k] = stdSmp.xyz[k] - recipe.xyz[k]
        newRecipe = colorCombine.calcAddRecipeBySpider(deltaXYZ, testConc, base, factor)
        testConc = newRecipe[0]

        #############################################################################################
        ########                            基于计算的配方                                ###########
        #############################################################################################
        # 计算当前浓度下的成分的K/Sw, S/Sw
        colorCombine.calcDeltaE(testConc, base)
        for k in range(1, 4, 1):
            tmpConc = testConc[k] / sum(testConc) * 100
            if tmpConc == 0:
                continue
            formula.colorantsComp[k].colorants.calcKSBySw(tmpConc)                     # 计算对应浓度的单位吸收系数和单位散射系数；    采用插值；

        for k in range(4):
            formula.colorantsComp[k].quality = testConc[k]
        formula.calcKByS()                                                              # 混合计算公式， 得到K/S；
        formula.calcRint(group.K1)                                                      # 计算无限厚情况下的反射率；
        formula.calcSDMix(group.pureWhite)                                              # 计算厚度参数；   这个得精度影响了最终的结果；
        for k in range(32):
            tmp1 = (base.ref[k] - formula.rint[k])/(formula.rint[k] * formula.rint[k])
            try:
                tmpSD = math.pow(math.e, formula.SDMix[k] * (1/formula.rint[k] - formula.rint[k]))
            except:
                pass
            tmp2 = (1/formula.rint[k] - base.ref[k]) * tmpSD
            tmp3 = base.ref[k] - formula.rint[k] + tmp2
            formula.ref[k] = formula.rint[k] * (tmp1 + tmp2) / tmp3
        formula.calcRef(group.K1, colorCombine.cfg)                                       # 反算背景下的总反射率；

        fig1 = plt.figure(i+1, figsize=(12, 10))
        x = range(32)
        plt.title(tmpName)
        plt.plot(x, stdSmp.ref, linestyle='-', alpha=0.8, color='r')
        plt.plot(x, formula.ref, linestyle='-', alpha=0.8, color='g')
        plt.plot(x, recipe.ref, linestyle='-', alpha=0.8, color='b')
        plt.savefig("img/" + tmpName + ".jpg")

